<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="会员姓名" prop="memberName">
                <a-input v-model="queryParam.memberName" placeholder="请输入会员姓名" allow-clear/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="会员学号" prop="numberNum">
                <a-input v-model="queryParam.numberNum" placeholder="请输入会员学号" allow-clear/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="会员性别" prop="memberSex">
                  <a-select placeholder="请选择会员性别" v-model="queryParam.memberSex" style="width: 100%" allow-clear>
                    <a-select-option v-for="(d, index) in memberSexOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="会员专业" prop="memberSubject">
                  <!--                  <a-input v-model="queryParam.memberSubject" placeholder="请输入会员专业" allow-clear/>-->
                  <a-cascader :options="subjectOptions" :field-names="fieldName" placeholder="请选择会员专业" @change="subjectSelected" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="会员班级" prop="memberClass">
                  <a-input v-model="queryParam.memberClass" placeholder="请输入会员班级" allow-clear/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="会员时长" prop="memberDuration">
                  <!--                  <a-input v-model="queryParam.memberDuration" placeholder="请输入会员服务时长" allow-clear/>-->
                  <a-input-number v-model="queryParam.durationMin" style="width: 100px; text-align: center" placeholder="最小值" />
                  <a-input style="width: 30px; border-left: 0; pointer-events: none; backgroundColor: #fff" placeholder="~" disabled/>
                  <a-input-number v-model="queryParam.durationMax" style="width: 100px; text-align: center; border-left: 0" placeholder="最大值" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="会员昵称" prop="memberNickname">
                  <a-input v-model="queryParam.memberNickname" placeholder="请输入会员昵称" allow-clear/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作 -->
      <div class="table-operations">
        <!--        <a-button type="primary" @click="$refs.createForm.handleAdd()" v-hasPermi="['angel:member:add']">-->
        <!--          <a-icon type="plus" />新增-->
        <!--        </a-button>-->
        <a-button type="primary" :disabled="single" @click="$refs.createForm.handleUpdate(undefined, ids)" v-hasPermi="['angel:member:edit']">
          <a-icon type="edit" />修改
        </a-button>
        <a-button type="danger" :disabled="multiple" @click="handleDelete" v-hasPermi="['angel:member:remove']">
          <a-icon type="delete" />删除
        </a-button>
        <a-button type="primary" @click="handleExport" v-hasPermi="['angel:member:export']">
          <a-icon type="download" />导出
        </a-button>
        <a-button
          type="dashed"
          shape="circle"
          :loading="loading"
          :style="{float: 'right'}"
          icon="reload"
          @click="getList" />
      </div>
      <!-- 增加修改 -->
      <create-form
        ref="createForm"
        :memberSexOptions="memberSexOptions"
        :memberTypeOptions="memberTypeOptions"
        @ok="getList"
      />
      <!-- 数据展示 -->
      <a-table
        :loading="loading"
        :size="tableSize"
        rowKey="memberId"
        :columns="columns"
        :data-source="list"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :pagination="false">
        <span slot="memberSex" slot-scope="text, record">
          {{ memberSexFormat(record) }}
        </span>
        <span slot="memberAvatarUrl" slot-scope="text">
          <a-avatar shape="square" size="large" :src="text" icon="user" />
        </span>
        <span slot="operation" slot-scope="text, record">
          <a-divider type="vertical" v-hasPermi="['angel:member:edit']" />
          <a @click="handleApply(record, 2)" v-hasPermi="['angel:member:edit']">
            <!--            <a-icon type="edit" />通过-->
            <a-button type="primary" size="small" shape="circle">
              <a-icon type="check" />
            </a-button>
          </a>
          <a-divider type="vertical" v-hasPermi="['angel:member:edit']" />
          <a @click="handleApply(record, 3)" v-hasPermi="['angel:member:edit']">
            <!--            <a-icon type="edit" />不通过-->
            <a-button type="danger" size="small" shape="circle">
              <a-icon type="close" />
            </a-button>
          </a>
          <a-divider type="vertical" v-hasPermi="['angel:member:edit']" />
          <a @click="$refs.createForm.handleUpdate(record, undefined)" v-hasPermi="['angel:member:edit']">
            <a-icon type="edit" />详细信息
          </a>
          <!--          <a-divider type="vertical" v-hasPermi="['angel:member:remove']" />-->
          <!--          <a @click="handleDelete(record)" v-hasPermi="['angel:member:remove']">-->
          <!--            <a-icon type="delete" />删除-->
          <!--          </a>-->
        </span>
      </a-table>
      <!-- 分页 -->
      <a-pagination
        class="ant-table-pagination"
        show-size-changer
        show-quick-jumper
        :current="queryParam.pageNum"
        :total="total"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        @showSizeChange="onShowSizeChange"
        @change="changeSize"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { listMember, delMember, exportMember, updateMember } from '@/api/angel/member'
import { listSubject } from '@/api/angel/subject'
import CreateForm from './modules/CreateForm'

export default {
  name: 'Member',
  components: {
    CreateForm
  },
  data () {
    return {
      list: [],
      fieldName: {
        value: 'id',
        label: 'subjectName',
        children: 'children'
      },
      subjectOptions: [],
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      loading: false,
      total: 0,
      // 会员性别字典
      memberSexOptions: [],
      // 会员类型字典
      memberTypeOptions: [],
      // 查询参数
      queryParam: {
        memberAppId: null,
        memberName: null,
        memberSex: null,
        memberSubject: null,
        memberClass: null,
        memberHobby: null,
        memberType: 1,
        memberDuration: null,
        numberNum: null,
        memberAvatarUrl: null,
        memberNickname: null,
        durationMin: null,
        durationMax: null,
        pageNum: 1,
        pageSize: 10
      },
      columns: [
        // {
        //   title: 'id',
        //   dataIndex: 'memberId',
        //   ellipsis: true,
        //   align: 'center'
        // },
        {
          title: 'openid',
          dataIndex: 'memberAppId',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '姓名',
          dataIndex: 'memberName',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '性别',
          dataIndex: 'memberSex',
          scopedSlots: { customRender: 'memberSex' },
          ellipsis: true,
          align: 'center'
        },
        // {
        //   title: '专业',
        //   dataIndex: 'memberSubject',
        //   ellipsis: true,
        //   align: 'center'
        // },
        // {
        //   title: '班级',
        //   dataIndex: 'memberClass',
        //   ellipsis: true,
        //   align: 'center'
        // },
        // {
        //   title: '爱好',
        //   dataIndex: 'memberHobby',
        //   ellipsis: true,
        //   align: 'center'
        // },
        // {
        //   title: '会员类型',
        //   dataIndex: 'memberType',
        //   scopedSlots: { customRender: 'memberType' },
        //   ellipsis: true,
        //   align: 'center'
        // },
        {
          title: '会员服务时长',
          dataIndex: 'memberDuration',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '会员学号',
          dataIndex: 'numberNum',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '用户头像',
          dataIndex: 'memberAvatarUrl',
          scopedSlots: { customRender: 'memberAvatarUrl' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '会员昵称',
          dataIndex: 'memberNickname',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: '18%',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ]
    }
  },
  filters: {
  },
  created () {
    this.getList()
    this.getSubjects()
    this.getDicts('sys_user_sex').then(response => {
      this.memberSexOptions = response.data
    })
    this.getDicts('member_type').then(response => {
      this.memberTypeOptions = response.data
    })
  },
  computed: {
  },
  watch: {
  },
  methods: {
    subjectSelected (value) {
      this.queryParam.memberSubject = value[value.length - 1]
    },
    getSubjects () {
      listSubject().then(response => {
        this.subjectOptions = response.data
      })
    },
    /** 查询会员信息列表 */
    getList () {
      this.loading = true
      this.queryParam.memberType = 1
      listMember(this.queryParam).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 会员性别字典翻译
    memberSexFormat (row, column) {
      return this.selectDictLabel(this.memberSexOptions, row.memberSex)
    },
    // 会员类型字典翻译
    memberTypeFormat (row, column) {
      return this.selectDictLabel(this.memberTypeOptions, row.memberType)
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        memberAppId: undefined,
        memberName: undefined,
        memberSex: undefined,
        memberSubject: undefined,
        memberClass: undefined,
        memberHobby: undefined,
        memberType: undefined,
        memberDuration: undefined,
        numberNum: undefined,
        memberAvatarUrl: undefined,
        memberNickname: undefined,
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.memberId)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const memberIds = row.memberId || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '当前选中姓名为' + row.memberName + '的数据',
        onOk () {
          return delMember(memberIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          return exportMember(that.queryParam)
            .then(response => {
              that.download(response.msg)
              that.$message.success(
                '导出成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    /** 审核按钮操作 */
    handleApply (row, type) {
      var that = this
      const member = row || this
      member.memberType = type
      this.$confirm({
        title: '确认操作所选中数据?',
        content: '当前选中姓名为' + row.memberName + '的数据',
        onOk () {
          return updateMember(member)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '修改成功',
                3
              )
            })
        },
        onCancel () {}
      })
    }
  }
}
</script>
